package whf.framework.util.var;

import java.util.Enumeration;
import java.util.Map;

import javax.servlet.http.HttpSession;

import whf.framework.util.Utils;
import whf.framework.web.WebContextHolder;

/**
 * 基于HttpSession的变量容器
 * @author wanghaifeng
 * @create Jan 24, 2007 9:40:22 AM
 * 
 */
public class SessionVariableHolder implements VariableHolder {
	private HttpSession session;
	
	public SessionVariableHolder(){
		this.session = WebContextHolder.getWebContext().getSession();
	}
	
	public SessionVariableHolder(HttpSession session){
		this.session = session;
	}

	/* (non-Javadoc)
	 * @see whf.framework.util.var.VariableHolder#getAny()
	 */
	public Object getAny() {
		throw new NotImplementionException("Method getAny() not implemention in this class!");
	}

	/* (non-Javadoc)
	 * @see whf.framework.util.var.VariableHolder#getHolder()
	 */
	public Map<String, Object> getHolder() {
		Map<String, Object> result = Utils.newHashMap();
		Enumeration names = this.session.getAttributeNames();
		while(names.hasMoreElements()){
			String name = (String)names.nextElement();
			Object value = this.session.getAttribute(name);
			result.put(name, value);
		}
		return result;
	}

	/* (non-Javadoc)
	 * @see whf.framework.util.var.VariableHolder#getVariable(java.lang.String)
	 */
	public Object getVariable(String key) {
		return this.session.getAttribute(key);
	}

	
	/* (non-Javadoc)
	 * @see whf.framework.util.var.VariableHolder#getScope()
	 */
	public VariableHolderScope getScope() {
		return VariableHolderScope.SESSION;
	}
}
